var app = getApp();
var util = require('../../utils/util.js');
var loginData = ""
Page({
  data: {
    user: [],
    is_bind: false,
    zhusu: ['', '不住', '单住', '合住'],
    zhusuValue: 0,
    xingbie: ['男', '女'],
    xingbieValue: 0,
    shopid: [],
    shopidValue: 0,
    shopname: [],
    shopnameValue: 0,
    pian: [],
    pianValue: 0,
    sheng: [],
    shengValue: 0,
    shi: [],
    shiValue: 0,
    sp_id: '',
    sp_name: '',
    spname: '',
    Gray: '',
    server: app._server,
    checkInValue: '',
    checkOutValue: '',
    checkOutStart: '',
    nowDate: util.formatTime(new Date(), 'YYYY:mm:dd'),
    namelist: [],
    nameIndex: 0,
    waylist: [{
      value: '自行前往',
      checked: 'true'
    }, {
      value: '跟车前往'
    }, {
      value: '其它'
    }],
    signlist: [{
      name: '',
      sex: '男',
      phone: '',
      sleep: '',
      checkInTime: '',
      checkOutTime: '',
      Vehicle: '自行前往',
      remark: '',
      WuHan: '',
      JieChu: '',
      ZhenZhuang: '',
      TiWen: '',
      symptomsList: [{
        value: '没有出现症状',
        checked: false,
        disabled: false,
      }, {
        value: '感冒样症状：乏力、咳嗽、发烧、肌肉痛、头痛',
        checked: false,
        disabled: false,
      }, {
        value: '喘憋、呼吸急促',
        checked: false,
        disabled: false,
      }, {
        value: '恶心呕吐、腹泻',
        checked: false,
        disabled: false,
      }, {
        value: '心慌、胸闷',
        checked: false,
        disabled: false,
      }],
    }],
  },
  onLoad: function(options) {
    let _this = this
    var sp_id = options.ID.split(',')[0];
    var spname = options.ID.split(',')[1] + '(第' + options.ID.split(',')[2] + '期)';
    wx.setNavigationBarTitle({
      title: spname
    })
    loginData = wx.getStorageSync('login')
    console.log('loginData', loginData)
    // 获取当前可报名人员
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetForecastNamesList',
        shopid: loginData.ShopID,
        sp_id: sp_id
      },
      success(res) {
        console.log('当前可报名人员', res)
        if (res.data.length > 0) {
          _this.setData({
            namelist: res.data
          })
        }
      }
    })
    _this.setData({
      sp_id: sp_id,
      spname: spname,
      user: app._user
    });
    _this.GetAllUser();
  },
  GetAllUser: function() {
    var that = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'Getstr'
      },
      success: (res) => {
        wx.hideToast();
        that.setData({
          pian: res.data.Pian
        })
        wx.hideToast();
      },
      fail: function(res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    });
  },
  zhusuchange: function(e) {
    let index = e.currentTarget.dataset.index
    let zhusuSelected = this.data.zhusu[e.detail.value]
    this.setData({
      ['signlist[' + index + '].sleep']: zhusuSelected
    })
  },
  xingbiechange: function(e) {
    const index = e.currentTarget.dataset.index
    let sexSelected = this.data.xingbie[e.detail.value]
    this.setData({
      ['signlist[' + index + '].sex']: sexSelected
    })
  },

  // 入住日期选择
  checkInDateChange(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      checkOutStart: e.detail.value,
      ['signlist[' + index + '].checkInTime']: e.detail.value
    })
  },
  // 退房日期选择
  checkOutDateChange(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      ['signlist[' + index + '].checkOutTime']: e.detail.value
    })
  },
  // 姓名选择
  nameChange(e) {
    let index = e.currentTarget.dataset.index
    let nameSelected = this.data.namelist[e.detail.value].Name
    if (this.data.signlist.length > 0) {
      for (let i = 0; i < this.data.signlist.length; i++) {
        if (nameSelected == this.data.signlist[i].name) {
          let _this = this
          wx.showModal({
            title: '提示',
            content: '该成员已填写报名信息，请勿重复选择',
            showCancel: false,
            confirmColor: '#cc0000',
            success: function() {
              _this.setData({
                ['signlist[' + index + '].name']: ""
              })
            }
          })
          break
        }
      }
    }
    this.setData({
      ['signlist[' + index + '].name']: nameSelected
    })
  },
  // 前往方式选择
  wayChange: function(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      ['signlist[' + index + '].Vehicle']: e.detail.value
    })
  },
  // 添加报名信息
  addSignUp() {
    let data = {
      name: '',
      sex: '男',
      phone: '',
      sleep: '',
      checkInTime: '',
      checkOutTime: '',
      Vehicle: '自行前往',
      remark: '',
      WuHan: '',
      JieChu: '',
      ZhenZhuang: '',
      TiWen: '',
      symptomsList: [{
        value: '没有出现症状',
        checked: false,
        disabled: false,
      }, {
        value: '感冒样症状：乏力、咳嗽、发烧、肌肉痛、头痛',
        checked: false,
        disabled: false,
      }, {
        value: '喘憋、呼吸急促',
        checked: false,
        disabled: false,
      }, {
        value: '恶心呕吐、腹泻',
        checked: false,
        disabled: false,
      }, {
        value: '心慌、胸闷',
        checked: false,
        disabled: false,
      }],
    }
    let signlist = this.data.signlist
    signlist.push(data)
    this.setData({
      signlist: signlist
    })
  },
  // 删除报名信息
  deleteSignUp(e) {
    const index = e.currentTarget.dataset.index
    let signlist = this.data.signlist
    signlist.splice(index, 1)
    this.setData({
      signlist
    })
  },
  // 提交报名信息
  signupSubmit() {
    let _this = this
    let data = _this.data.signlist
    let sortData = data.sort()
    var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/
    let dataLength = data.length
    let forNum = 0
    for (let i = 0; i < data.length; i++) {
      if (data[i].name == '') {
        app.showErrorModal('姓名不能为空', '提醒')
        break
      }
      // 判断是否选择了相同的姓名
      if (data.length > 1) {
        for (let j = i + 1; j < data.length; j++) {
          console.log(data[i].name, data[j].name)
          if (data[i].name === data[j].name && (data[i].name != '' && data[j].name != '')) {
            app.showErrorModal('同个姓名(' + data[i].name + ')无法填写两次报名信息', '提醒')
            break
          }
        }
      }
      if (data[i].phone == '') {
        app.showErrorModal(data[i].name + '的电话号码不能为空', '提醒')
        break
      } else if (data[i].phone.length != 11) {
        app.showErrorModal(data[i].name + '的电话号码长度不能大于小于11位数', '提醒')
        break
      } else if (!myreg.test(data[i].phone)) {
        app.showErrorModal(data[i].name + '的电话号码格式不正确', '提醒')
        break

      }
      if (data[i].sleep == '') {
        app.showErrorModal(data[i].name + '的住宿不能为空', '提醒')
        break
      } else if (data[i].sleep == '单住' || data[i].sleep == '合住') {
        if (data[i].checkInTime == '') {
          app.showErrorModal(data[i].name + '的入住日期不能为空', '提醒')
          break
        }
        if (data[i].checkOutTime == '') {
          app.showErrorModal(data[i].name + '的退房日期不能为空', '提醒')
          break
        }
      }
      if (data[i].WuHan == '') {
        app.showErrorModal('防疫期间请将报名学员的近期情况填写完毕后提交！', '提醒')
        break
      }
      if (data[i].JieChu == '') {
        app.showErrorModal('防疫期间请将报名学员的近期情况填写完毕后提交！', '提醒')
        break
      }
      if (data[i].ZhenZhuang == '') {
        app.showErrorModal('防疫期间请将报名学员的近期情况填写完毕后提交！', '提醒')
        break
      }
      if (data[i].TiWen == '') {
        app.showErrorModal('防疫期间请将报名学员的近期情况填写完毕后提交！', '提醒')
        break
      }
      forNum++
    }
    // 当报名信息全部都填写完毕后 才请求报名接口 若有两个报名信息 只填写了一个则不请求
    if (forNum == dataLength) {
      wx.showLoading({
        title: '加载中',
      })
      for (let i = 0; i < data.length; i++) {
        let submitData = {
          method: 'signup',
          sp_id: _this.data.sp_id,
          openid: _this.data.user.openid,
          Pian: loginData.Pian,
          Sheng: loginData.Sheng,
          Shi: loginData.Shi,
          ShopName: loginData.ShopName,
          ShopID: loginData.ShopID,
          name: data[i].name,
          xingbie: data[i].sex,
          Zhiwei: 'SA',
          Phone: data[i].phone,
          Zhusu: data[i].sleep,
          CheckinTime: data[i].checkInTime,
          CheckoutTime: data[i].checkOutTime,
          Vehicle: data[i].Vehicle,
          remark: data[i].remark,
          WuHan: data[i].WuHan,
          JieChu: data[i].JieChu,
          ZhenZhuang: data[i].ZhenZhuang,
          TiWen: data[i].TiWen,
        }
        console.log('submitData', submitData)
        wx.request({
          url: app._server + '/Weixin_Hodna/WxLogin.ashx',
          data: submitData,
          success: function(res) {
            if (i == data.length - 1) {
              wx.hideLoading()
            }
            console.log('报名情况', res)
            if (res.data.insertsignup) {
              wx.showModal({
                title: '提示',
                content: '报名成功',
                showCancel: false,
                confirmColor: '#cc0000',
                success(res) {
                  if (res.confirm) {
                    wx.switchTab({
                      url: '../mine/mine',
                    })
                  }
                }
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '报名失败，请检查信息是否填写无误',
                showCancel: false,
                confirmColor: '#cc0000'
              })
            }

          }
        })
      }
    }

  },
  // 获取电话号码
  phoneInput(e) {
    let index = e.currentTarget.dataset.index
    let phone = e.detail.value
    this.setData({
      ['signlist[' + index + '].phone']: phone
    })
  },
  // 获取备注信息
  remarkInput(e) {
    let index = e.currentTarget.dataset.index
    let remark = e.detail.value
    this.setData({
      ['signlist[' + index + '].remark']: remark
    })
  },
  // 居住武汉
  onLiveWuhan(e) {
    let index = e.currentTarget.dataset.index
    let value = e.detail.value
    this.setData({
      ['signlist[' + index + '].WuHan']: value
    })
  },
  // 接触武汉患者
  onContactWuhan(e) {
    let index = e.currentTarget.dataset.index
    let value = e.detail.value
    this.setData({
      ['signlist[' + index + '].JieChu']: value
    })
  },
  // 症状
  onSymptomsChange(e) {
    let index = e.currentTarget.dataset.index
    let value = e.detail.value
    let symptomsList = this.data.signlist[index].symptomsList
    let selected = ""
    if (value.length > 0) {
      for (let i = 0; i < value.length; i++) {
        if (value[i] == '没有出现症状') {
          value = ['没有出现症状']
          for (let j = 0; j < symptomsList.length; j++) {
            if (symptomsList[j].value != '没有出现症状') {
              symptomsList[j].disabled = true
              symptomsList[j].checked = false
            } else {
              symptomsList[j].checked = true
              symptomsList[j].disabled = false
            }
          }
          this.setData({
            ['signlist[' + index + '].symptomsList']: symptomsList
          })
        }
      }
      for (let y = 0; y < value.length; y++) {
        selected += `${value[y]},`
      }
      selected = selected.substring(0, selected.length - 1)
    } else {
      for (let j = 0; j < symptomsList.length; j++) {
        symptomsList[j].disabled = false
        symptomsList[j].checked = false
      }
      this.setData({
        ['signlist[' + index + '].symptomsList']: symptomsList
      })
    }
    this.setData({
      ['signlist[' + index + '].ZhenZhuang']: selected
    })
  },
  // 体温
  onTemperature(e) {
    let index = e.currentTarget.dataset.index
    let value = e.detail.value
    this.setData({
      ['signlist[' + index + '].TiWen']: value
    })
  },
})